#include "carsql.h"
#include "qdatetime.h"
#include<math.h>
carsql::carsql(QObject *parent) : QObject(parent)
{
    CarSql = QSqlDatabase::addDatabase("QSQLITE");

    /* ARM板子上的数据库位置和ubuntu数据库位置分开 */
    if(QSysInfo::buildCpuArchitecture() == "arm"){
        CarSql.setDatabaseName("/bishe/sql/carsql.db");
    }
    else{
        CarSql.setDatabaseName("/home/lfl/bishe/sql/test.db");
    }

    if(CarSql.open()){
        qDebug() << "打开数据库成功！"<<endl;
    }
    else{
        qDebug() << "打开数据库失败！"<<endl;
    }
    this->FaceMask = "None";
    this->CarID = "";
    this->Tmp = 0;
    this->Price = 5;
}

void carsql::showalldata()
{
    qDebug()<< this->CarID <<endl;
    qDebug()<< this->Tmp <<endl;
    qDebug()<< this->FaceMask <<endl;
}

void carsql::deletesqldata(int row)
{
    QSqlQuery query;
    QString sql = QString("delete from carsql where rowid in(select rowid from carsql limit 1 offset %1);").arg(row);
    qDebug() << sql << endl;
    if(query.exec(sql)){
        qDebug() << "OK!" << endl;
    }
    else{
        qDebug() << "error!" << endl;
    }

}

void carsql::deletealldata()
{
    QSqlQuery query;
    QString sql = "delete from carsql";
    if(query.exec(sql)){
        qDebug() << "OK!" << endl;
    }
    else{
        qDebug() << "error!" << endl;
    }
}


void carsql::StatisticalTime(QString carID)
{
    if(QSysInfo::buildCpuArchitecture() == "arm"){
        QSqlQuery query;
        // qDebug() <<query << endl;
        query.exec(QString("select * from carsql where flag = '未缴费' and carID = '%1';").arg(carID));
        query.first();
        //qDebug() <<query.first() << endl;
        QVariant StartTimedata = query.value("time1");
        QVariant EndTimedata   = query.value("time2");

        /* 计算时间差 */
        QDateTime StartTime = QDateTime::fromString(StartTimedata.toString(),"yyyy-MM-dd HH:mm:ss");
        QDateTime EndTime   = QDateTime::fromString(EndTimedata.toString(),"yyyy-MM-dd HH:mm:ss");

        float hour = StartTime.secsTo(EndTime) / (60.0 * 60.0);
        this->money = ceil(hour) * this->Price;
        query.exec(QString("update carsql set flag = '%1' where  flag = '未缴费' and carID = '%2'").arg("已缴费").arg(carID));
    }
    else{
        QSqlQuery query;
        // qDebug() <<query << endl;
        query.exec(QString("select * from test where flag = '未缴费' and carID = '%1';").arg(carID));
        query.first();
        //qDebug() <<query.first() << endl;
        QVariant StartTimedata = query.value("time1");
        QVariant EndTimedata   = query.value("time2");

        /* 计算时间差 */
        QDateTime StartTime = QDateTime::fromString(StartTimedata.toString(),"yyyy-MM-dd HH:mm:ss");
        QDateTime EndTime   = QDateTime::fromString(EndTimedata.toString(),"yyyy-MM-dd HH:mm:ss");

        float hour = StartTime.secsTo(EndTime) / (60.0 * 60.0);
        this->money = ceil(hour) * this->Price;
        query.exec(QString("update test set flag = '%1' where  flag = '未缴费' and carID = '%2'").arg("已缴费").arg(carID));
    }

}

unsigned int carsql::GetPrice()
{
    return this->Price;
}

void carsql::AddData2Sqltest()
{
    if((this->CarID != "") && (this->FaceMask != "None") && (this->Tmp != 0)){
        /* 实例一个执行sql语句对象 */
        QSqlQuery query(CarSql);
        if(QSysInfo::buildCpuArchitecture() == "arm"){
            query.exec(QObject::tr("insert into carsql values ('1','%1','%2','无','%3','%4','未缴费');").arg(this->CarID).arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")).arg(this->FaceMask).arg(this->Tmp));
        }
        else{
            query.exec(QObject::tr("insert into test values ('1','%1','%2','无','%3','%4','未缴费');").arg(this->CarID).arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")).arg(this->FaceMask).arg(this->Tmp));
        }
        qDebug()<< "Sql save ok!" <<endl;
        /* 欢迎光临 */
        emit this->SeedTTS("/bishe/yuyin/car_id.wav");
        emit this->SeedTTS("/bishe/yuyin/welcome.wav");
    }
}

void carsql::SetPrice(int Price)
{
    this->Price = Price;
}

void carsql::GetData(QString data)
{
    QSqlQuery query;
    /* 获取车牌信息 */
    if(data.startsWith("CarID:")){
        this->CarID = data.mid(data.indexOf(":") + 1);
        /* 查询数据库是否存在此车牌且没有缴费 */
        if(QSysInfo::buildCpuArchitecture() == "arm"){
            query.exec(QString("select * from carsql where flag = '未缴费' and carID = '%1'").arg(this->CarID));
            if(query.first()){
                qDebug()<< "未缴费" << endl;
                /* 记录出库时间 */
                query.exec(QString("update carsql set time2 = '%1' where flag = '未缴费' and carID = '%2'").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")).arg(this->CarID));
                qDebug()<< "chuku ok!" <<endl;
                this->StatisticalTime(this->CarID);
                qDebug()<< this->money <<endl;
                /* TTS  */
                emit this->SeedPriceTTS(this->money);
                /* 开闸 */
                emit this->Seedimt("CMD:Open_out");
                /* TTS */
                emit this->SeedTTS("/bishe/yuyin/goodluck.wav");
            }
            else{
                qDebug()<< "初次或者二次进入" <<endl;
                /* TTS */
                emit this->SeedTTS("/bishe/yuyin/tmp.wav");
                /* 准备测温 */
                emit this->Seedimt("tmp_ready");
            }
        }
        else{
            query.exec(QString("select * from test where flag = '未缴费' and carID = '%1'").arg(this->CarID));
            if(query.first()){
                qDebug()<< "未缴费" << endl;
                /* 记录出库时间 */
                query.exec(QString("update test set time2 = '%1' where flag = '未缴费' and carID = '%2'").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")).arg(this->CarID));
                qDebug()<< "chuku ok!" <<endl;
                this->StatisticalTime(this->CarID);
                qDebug()<< this->money <<endl;
                /* TTS  */
                emit this->SeedPriceTTS(this->money);
                /* 开闸 */

                /* TTS */
                emit this->SeedTTS("/bishe/yuyin/goodluck.wav");
            }
            else{
                qDebug()<< "初次或者二次进入" <<endl;
                /* TTS */
                emit this->SeedTTS("/bishe/yuyin/tmp.wav");
                /* 准备测温 */
                emit this->Seedimt("tmp_ready");
            }
        }
    }
    /* 获取体温信息 */
    if(data.startsWith("Tmp:")){
        this->Tmp = (data.mid(data.indexOf(":") + 1)).toFloat();
        // qDebug()<< Tmp <<endl;
    }
    /* 获取口罩信息 */
    if(data.startsWith("Mask:")){
        this->FaceMask = data.mid(data.indexOf(":") + 1);
        this->AddData2Sqltest();
        /* 开闸 */
        this->Seedimt("CMD:Open_in");

    }
}
